//
//  CommonAPI.m
//  AppMua5
//
//  Created by duong nguyen on 9/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CommonAPI.h"

@implementation CommonAPI

// Translate money to text format: 1000=>1.000d
-(NSString *)translateMoneyToTextFormat:(NSString *)moneyStr{
    int count=[moneyStr length];
    NSMutableString*temp=[[NSMutableString alloc]init];
    NSString *result=[[NSString alloc]init];
    int total=(int)count/3;
    int du=count%3;
    int i;
    int begin=0;
    
    if(total==0)
        return [NSString stringWithString:@"0"];
    
    switch (du) {
        case 2:
            for (i=0; i<=total; i++) {
                if (i==0) {
                    result=[moneyStr substringWithRange:NSMakeRange(begin, 2)];
                    [temp appendString:result];
                    [temp appendString:@","];
                    begin=2;
                }else {
                    result=[moneyStr substringWithRange:NSMakeRange(begin,3)];
                    [temp appendString:result];
                    if (i!=total) {
                        [temp appendString:@","];
                    }
                    begin+=2;
                }
            }
            break;
        case 1:
            for (i=0; i<=total; i++) {
                if (i==0) {
                    result=[moneyStr substringWithRange:NSMakeRange(begin, 1)];
                    [temp appendString:result];
                    [temp appendString:@","];
                    begin=1;
                }else {
                    result=[moneyStr substringWithRange:NSMakeRange(begin,3)];
                    [temp appendString:result];
                    if (i!=total) {
                        [temp appendString:@","];
                    }
                    begin+=2;
                }
            }
            break;
            
        case 0:
            for (i=0; i<total; i++) {
                result=[moneyStr substringWithRange:NSMakeRange(begin,3)];
                [temp appendString:result];
                if (i!=total-1) {
                    [temp appendString:@","];
                }
                begin+=2;
            }
            break;
        default:
            break;
    }
    return [NSString stringWithFormat:@"%@",temp];
}

@end
